<?php

namespace App\Http\Controllers\Api\User;

use Illuminate\Http\Request;
use App\Http\Controllers\Controller;
use App\Http\Requests\Api\ApiRequest;
use App\Repos\QuestionRepo;

class QuestionController extends Controller
{
    // 别人对我的提问
    public function index(ApiRequest $request)
    {
    	$rs['status'] = true;
   		$where['to_user_id'] = $request->user_id;
   		//dd($where);
   		$list = QuestionRepo::getPages($where,10);
   		$rs['data'] = $list;
   		return response()->json($rs);
    }

    // 我发起提问
    public function save(ApiRequest $request)
    {
      $rs['status'] = true;
      $data = $request->pure();
      $data['from_user_id'] = $request->user_id;
      $data['to_user_id']   = $request->to_user_id;
      $data['status']  = 0;
      $data['is_read'] = 0;

      $flag = QuestionRepo::save($data);
      if($flag['status'] == false) {
        $rs['status'] = false;
        $rs['msg'] = $flag['msg'];
        return response()->json($rs);
      }
      
      return response()->json($rs);
    }

    // 回复问题
    public function feedback(ApiRequest $request)
    {
    	$rs['status'] = true;
   		$where['to_user_id'] = $request->user_id;
   		$where['i.id'] = $request->id;
   		$data = QuestionRepo::find($where);
   		if($data) {
   			$update['status']   = 1;
        $update['is_read']  = 1;
   			$update['feedback'] = $request->feedback;
   			$update['feedbacked_at'] = date('Y-m-d H:i:s');
        $map['id'] = $request->id;
   			QuestionRepo::feedback($map,$update);
   			return response()->json($rs);
   		}
   		$rs['status'] = false;
   		$rs['msg'] = '回复失败';
   		return response()->json($rs);

    }

    public function show(ApiRequest $request)
    {
    	$rs['status'] = true;
    	$where['i.id'] = $request->id;
    	$where['to_user_id'] = $request->user_id;
   		$rs['data'] = QuestionRepo::find($where);
      QuestionRepo::update(['id'=>$request->id],['is_read'=>1]);
   		return response()->json($rs);
    }
}
